<?php

defined( '_JEXEC' ) or  die('Restricted access');

jimport( 'joomla.event.plugin' );



class plgContentShareme extends JPlugin

{

	function plgContentShareme( &$subject, $params )

		{

			parent::__construct( $subject, $params );

			global $mainframe;

			$this->_plugin = JPluginHelper::getPlugin( 'content', 'shareme' );

			$this->_params = new JParameter( $this->_plugin->params );

			

			$config_style = $this->_params->get( 'shareme_button_style', 'standard.css' );

			$config_user_style = $this->_params->get( 'shareme_css_user_template', '0' );

			if($config_user_style=='1'){

				$config_user_style = $this->_params->get( 'shareme_css_style_user' );

				$mainframe->addCustomHeadTag('<style type="text/css">'.$config_user_style.'</style>');

			}else{

				if ($config_style == 'compact')

				{

					$mainframe->addCustomHeadTag('<link href="plugins/content/shareme/compact.css" rel="stylesheet" type="text/css" />');

				}else{

					$mainframe->addCustomHeadTag('<link href="plugins/content/shareme/standard.css" rel="stylesheet" type="text/css" />');

				}

			}

		}


	function plgContentSharemeButton(&$article, $auto, $sharemestyle, $sharemewindow, $sharemecolor, $retweetname, $language, $hashtags, $locale, $tweet, $fbb, $buzz, $gtitle, $afterbefore)

	{

            	

            	$aShareme   = array();

            	$ashareme[] = '<div class="sharemebutton">';
              if ($tweet == 1){
              $ashareme[] = '<div class="sharemebuttont">';

              $ashareme[] = '<a href="http://twitter.com/share" class="twitter-share-button" data-url="'.$this->getArticleRoute($article).'" data-count="vertical" data-via="'.$retweetname.'" data-lang="'.$language.'">Tweet</a>';
                
            	$ashareme[] = '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';

            	$ashareme[] = '</div>';
              }
            	// FB Share starts      if ($loadjquery == 1)
             
		          if ($fbb == 1){
			        $ashareme[] = '<div class="sharemebuttonf">';
              $ashareme[] = '<script>var fbShare = {url: "'.$this->getArticleRoute($article).'"}</script><script src="http://widgets.fbshare.me/files/fbshare.js"></script>';
            	$ashareme[] = '</div>';
		          }		
            	
              
              // Google Buzz starts
              
              if($buzz == 1){
			        $aTweetme[] = "locale = '".$locale."';";
              $aTweetme[] = "gtitle= '".$gtitle."';";
            	$ashareme[] = '<div class="sharemebuttong">';
            	$ashareme[] = '<a title="'.$gtitle.'" class="google-buzz-button" href="'.$this->getArticleRoute($article).'" data-button-style="normal-count" data-locale="'.$locale.'"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script>';
            	$ashareme[] = '</div>';
              }
            	$ashareme[] = '</div>';
		          
              
            	if ($auto == 'auto')

            	{

            		if ($afterbefore == 'after'){

	            		$article->text = $article->text.implode("\n", $ashareme);

            		}else{

	            		$article->text = implode("\n", $ashareme).$article->text;

            		}

            	}

            	elseif ($auto == 'manual')

            	{

            		$regex = "{shareme}";

            		$AddButtonShareme = implode("\n", $aShareme);

            		

            		// find all instances of plugin and put in $matches

            	    	

            	    	preg_match_all( $regex, $article->text, $matches );

            	   		

            		// Number of plugins

            		  	

            		$count = count( $matches[0] );

            		if ( $count ) {

            			$article->text = eregi_replace($regex, $AddButtonShareme, $article->text);

            	    	}

      

		}

            	unset($aShareme);

            	return true;

	}

	function plgContentDraw(&$row, $config_position, $shareme, $config_user_style){

      		

    			$send = '<table width="100%" border="0"><tr valign="top">';

    			if( $config_position=='left')

    			{

    				$send .= '<td class="sharemebutton">';

    				$send .= $shareme.'</td><td class="space_right" style="width:100%;">';

    				echo $send; /* beginning of table */

    				$row->text = '</td></tr></table> </td></tr></table> <table width="100%"><tr><td>'.$row->text; /* building end of table */

    			}else{

    				$send .= '<td style="width:100%;">';

    				echo $send; /*  beginning of table */

    				$row->text = '</td></tr></table> </td><td style="width:100%; float: right;" class="sharemebutton">'.$shareme.'</td></tr> </table><table width="100%"><tr><td>'.$row->text; /* end of table */

    			}			

	}

	

	function getArticleRoute(&$obj, $encode = 'False') {

		$url = 'Error, No URL';

		if (! is_null ( $obj )) {

			require_once (JPATH_ROOT . DS . 'components' . DS . 'com_content' . DS . 'helpers' . DS . 'route.php');

			$url = JRoute::_ ( ContentHelperRoute::getArticleRoute ($obj->slug, $obj->catslug, $obj->sectionid));
      
			$uri = & JURI::getInstance ();

			$base = $uri->toString ( array ('scheme', 'host', 'port' ) );

			$url = $base . $url;

			$url = JRoute::_ ( $url, true, 0 );

			if($encode == 'True'){

				return urlencode(htmlentities( $url ));

			}else {

				return  $url;

			}

		}

		return $url;

	}



	function add_ending_slash($path){

 

    	$slash_type = (strpos($path, '\\')===0) ? 'win' : 'unix'; 

 

    	$last_char = substr($path, strlen($path)-1, 1);

 

    	if ($last_char != '/' and $last_char != '\\') {

        	// no slash:

        	$path .= ($slash_type == 'win') ? '\\' : '/';

    	}

 

    	return $path;

	}

	function plgShowInIntro(&$obj, &$params, &$inintro)

	{

		if ($inintro == '1')

		{

			return true;

		}

		else

		{

			

			if (JString::strlen(strip_tags($obj->fulltext)) !='0')

			{

				if ((JRequest :: getVar('view')) == 'article')

				{

					return true;				

				}else{

					return false;

				}

			}else{

				return true;

			}

		}

	}

	function onBeforeDisplayContent(&$row, &$params)

	{	

    		if( isset($row->author) ) {

			

    			if ($this->plgContentCheckSecCatArt($row) == false)

			{

          		$plugin = & JPluginHelper::getPlugin('content', 'shareme');

          		$pluginParams  = new JParameter( $plugin->params );

          		$pluginRegistry  = $pluginParams->_registry['_default']['data'];

          		$sAutoshareme = $pluginRegistry->auto_manual;

          		$sConfig_User_Style = $this->_params->get( 'shareme_css_user_template', '0' );

			$sShowInIntro = $this->_params->get( 'shareme_show_in_intro_text', '0' );

			$sShowFrontpage = $pluginRegistry->shareme_show_frontpage;

      			$sShowSection = $pluginRegistry->shareme_show_section;

	      		$sShowCate = $pluginRegistry->shareme_show_cate;

	      		if ($this->plgSharemeShowSecCatFP($sShowFrontpage, $sShowSection, $sShowCate) == true){

			if ($this->plgShowInIntro($row, $params, $sShowInIntro) == true)

			{

              			if ($sAutoShareme == 'title'){

          				$sSharemeButtonStyle = $pluginRegistry->shareme_button_style;

	          			$sSharemeWindow = $pluginRegistry->shareme_new_windows;

        	  			$sSharemeColor = $pluginRegistry->shareme_bg_color;

        	  			$sRetweetName = $pluginRegistry->retweetname;
                  
                  $sLanguage = $pluginRegistry->language;
                  
                  $sHashTags = $pluginRegistry->hashtags;
                  
                  $sLocale = $pluginRegistry->locale;
                  
                  $sTweet = $pluginRegistry->tweet;
                  
                  $sFbb = $pluginRegistry->fbb;
                  
                  $sBuzz = $pluginRegistry->buzz;
                                
                  $sGtitle = $pluginRegistry->gtitle;

        	  			$sSharemeButtonStyle = $pluginRegistry->shareme_button_style;

        	  			$sRetweetName = $pluginRegistry->retweetname;

        	  			$sSharemeButtonAling = $pluginRegistry->leftright;
                  if ($tweet == 1){
        	  			$shareme_source = '<a href="http://twitter.com/share" class="twitter-share-button" data-url="'.$this->getArticleRoute($article).'" data-count="vertical" data-via="'.$retweetname.'" data-lang="'.$language.'">Tweet</a>';
        	  			$shareme_source .= '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
                  }
        	  			// FB Share starts
                  if ($fbb == 1){
            	    $shareme[] = '<a name="fb_share" type="button_count" href="http://www.facebook.com/sharer.php">Share</a>';

				          $shareme[] = '<script type="text/javascript" src="http://static.ak.fbcdn.net/connect.php/js/FB.Share"></script>';
                  }
                  // Google Buzz starts
                  if ($buzz == 1){
            	    $ashareme[] = '<div class="sharemebuttong">';
                  $ashareme[] = '<a title="Post on Google Buzz" class="google-buzz-button" href="'.$this->getArticleRoute($article).'" data-button-style="normal-count" data-locale="en_ZA"></a><script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script>';
            	    $ashareme[] = '</div>';
                  }
        	  			$this->plgContentDraw($row, $sSharemeButtonAling, $shareme_source, $sConfig_User_Style);

        	  		}

			}

    			}

    			}

    		}

	}

	function plgContentCheckSecCatArt(&$row)

	{

		$plugin = & JPluginHelper::getPlugin('content', 'shareme');

		$pluginParams  = new JParameter( $plugin->params );

		$pluginRegistry  = $pluginParams->_registry['_default']['data'];

		$value_sec = 0;

		$value_cat = 0;

		$value_art = 0;

		if ($pluginRegistry->sectionid !=''){

	

			// Check accepted section	

  			$aAcceptedSectionsArray = array();

  			$aAcceptedSectionsArray = explode(',',$pluginRegistry->sectionid);

  			if(in_array($row->sectionid, $aAcceptedSectionsArray) != true){

   				$value_sec = '1';

  			}

  			unset($aAcceptedSectionsArray);

  		}

	  	

  			// Check accepted category

  		if ($pluginRegistry->catid !=''){

  			$aAcceptedCategoryArray = array();

  			$aAcceptedCategoryArray = explode(',',$pluginRegistry->catid);

  			if(in_array($row->catid, $aAcceptedCategoryArray) != true){

    				$value_cat = '1';

  			}

			unset($aAcceptedCategoryArray);

  		}

  			

  			

  			// Check ignored articles

  		if ($pluginRegistry->articleid !=''){

	  		$aIgnoredArticleArray = array();

  			$aIgnoredArticleArray = explode(',',$pluginRegistry->articleid);

  			if(in_array($row->id, $aIgnoredArticleArray) ){

				$value_art = '1';

	  		}

	  		unset($aIgnoredArticleArray);

  		}

  		if( ($value_sec==1) || ($value_cat==1) || ($value_art==1) ){

  			return true;

  		}else{

  			return false;

  		}

  		

  	}

  	function plgSharemeShowSecCatFP($fron_page, $sec, $cat)

	{

		$view_ddrfs = JRequest :: getVar('view');

		// echo $view_ddrfs;

		switch ($view_ddrfs)

		{

			case 'article':

				return true;

				break;

			case 'frontpage':

				if ($fron_page == '1')

				{

					return true;

				}else{

					return false;

				}

				break;

			case 'section':

				if ($sec == '1')

				{

					return true;

				}else{

					return false;

				}

				break;

			case 'category':

				if ($cat == '1')

				{

					return true;

				}else{

					return false;

				}

				break;

		}

	}

	function onPrepareContent( &$article, &$params, $limitstart )

	{

		    	$plugin = & JPluginHelper::getPlugin('content', 'shareme');

			$pluginParams  = new JParameter( $plugin->params );

			$pluginRegistry  = $pluginParams->_registry['_default']['data'];

			

			$sSharemeButtonStyle = $pluginRegistry->shareme_button_style;

			$sSharemeWindow = $pluginRegistry->shareme_new_windows;

			$sSharemeColor = $pluginRegistry->shareme_bg_color;

      $sTweet = $pluginRegistry->tweet;
      $sFbb = $pluginRegistry->fbb;
      $sBuzz = $pluginRegistry->buzz;
			$sRetweetName = $pluginRegistry->retweetname;
      $sLanguage = $pluginRegistry->language;
      $sLocale = $pluginRegistry->locale;
      $sTweet = $pluginRegistry->tweet;
      $sFbb = $pluginRegistry->fbb;
      $sBuzz = $pluginRegistry->buzz;
      $sGtitle = $pluginRegistry->gtitle;
			$sAutoShareme = $pluginRegistry->auto_manual;

			$sAfterBefore = $pluginRegistry->shareme_after_before;

			if ($sAutoShareme == 'title'){

				if ( $pluginParams->get( 'enabled', 1 ) ) {

					$regex = "#{shareme}#";

					$article->text = preg_replace( $regex, '', $article->text);

				}

				return true;

			}elseif ($sAutoShareme == 'auto'){

				

				if ($this->plgContentCheckSecCatArt($article) == true){

					return true;

				}

				if ( $pluginParams->get( 'enabled', 1 ) ) {

					$regex = "#{shareme}#";

					$article->text = preg_replace( $regex, '', $article->text);

		    		}		

			}

			

			// Add Shareme Button

		        $sShowInIntro = $this->_params->get( 'shareme_show_in_intro_text', '0' );

		        $sShowFrontpage = $pluginRegistry->shareme_show_frontpage;

      			$sShowSection = $pluginRegistry->shareme_show_section;

	      		$sShowCate = $pluginRegistry->shareme_show_cate;

		        if ($this->plgSharemeShowSecCatFP($sShowFrontpage, $sShowSection, $sShowCate) == true){

			if ($this->plgShowInIntro($article, $params, $sShowInIntro) == true){

		        	$this->plgContentSharemeButton($article, $sAutoShareme, $sSharemeButtonStyle, $sSharemeWindow, $sSharemeColor, $sRetweetName, $sLanguage, $sHashTags, $sLocale, $sTweet, $sFbb, $sBuzz, $sGtitle, $sAfterBefore);

		        }

		        }

	}

}

?>